/*
 * A container class, combining level containers to hold and manage the information needed for all levels of the game
 */

package com.anneric;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;

import android.content.res.AssetManager;
import android.util.Log;

import com.anneric.PicInfoStorage;
import com.anneric.Level;


public class LevelsContainer 
	implements Iterable<Level>{
	
	private ArrayList<Level> LevelContainer;
	private int current_level_index;
	private static final String DELIM = "###";
	private static final String DATAFILE = "LevelData.csv";

	
	// constructor	
	public LevelsContainer(){
		LevelContainer = new ArrayList<Level>();
		current_level_index = -1;
	}
	
	public LevelsContainer(LevelsContainer lvlcc){
		LevelContainer = lvlcc.LevelContainer;
		current_level_index = lvlcc.current_level_index;		
	}
	
	//
	// interfacing
	//
	
	
	public void addLevel(Level lvl){
		LevelContainer.add(lvl);
	}
	
	public final boolean isEmpty(){
		return LevelContainer.isEmpty();
	}
	
	public final int getAmountOfLevels(){
		return LevelContainer.size();
	}

	public final Level getNextLevel() {
		if (hasNextLevel()){
			++current_level_index;
			int j = current_level_index;
			return LevelContainer.get(j);
		} else {
			throw new NoSuchElementException();
		}
	}
	
	public final boolean hasNextLevel(){
		if( !isEmpty() & (current_level_index < LevelContainer.size()-1)  ){
			return true;
		}
		return false;
	}
	
	public final Level getCurrentLevel() {
		if (current_level_index > -1){
			return LevelContainer.get(current_level_index);
		} else {
			throw new NoSuchElementException();
		}
	}
	
	public void reset() {
		if (LevelContainer.size() >= 0){
			current_level_index = 0;
		} else {
			throw new NoSuchElementException("Cannot reset uninstanciated LevelLayer ArrayList.");
		}
	}
	
	
	//
	// fill class object
	//
	
	public void load(AssetManager am){
		try{
			InputStreamReader IS = new InputStreamReader( am.open(DATAFILE) );
			BufferedReader in = new BufferedReader(IS);
			load(in);
		} catch (IOException e){
			System.out.println(e.toString());
		}
	}
	
	public void load(BufferedReader in){
		try {
			String line;
			
			Level currLevel = new Level();
			PicInfoStorage currPIS;
			
			while ((line = in.readLine()) != null){
				
				// lines still belong to current level
				if (!line.equals(DELIM)){
					
					currPIS = createPIS(line);
					
					Log.d("LevelsContainer.java -> load(): adding PIS: ",line);
					// add the collected data as new layer to the current level
					currLevel.addLayer( currPIS  );
					
				// data delimiter found:
				//		return current Level and start a new one
				} else {
					addLevel(currLevel);
					currLevel = new Level();
				}
					
			} //while
			
			// add last Level (without DELIM)
			if (currLevel.getAmountOfLayers()!=0){
				addLevel(currLevel);
			}
		
			in.close();
			
		} catch (IOException e){
			System.out.println(e.toString());
		}
		
	}
	
	private PicInfoStorage createPIS(String line) throws IOException {
		// create a new PIS
		StringTokenizer line_toks = new StringTokenizer(line);
		PicInfoStorage currPIS = new PicInfoStorage();
		
		//... and fill it with the tokens of this line
		while (line_toks.hasMoreTokens()){
			currPIS.addInfo( line_toks.nextToken() );	
		}
		
		return currPIS;
	}

	//
	// Iterator
	//

	public Iterator<Level> iterator() {
		return LevelContainer.iterator();
	}
	
	public Iterator<Level> iterator(int index) {
		return LevelContainer.listIterator(index);
	}
	
}
